﻿using StackExchange.Redis;
namespace Jst.Core.Redis
{
    /// <summary>
    /// Redis缓存接口
    /// </summary>
    public interface IRedisOperationRepository
    {
        /// <summary>
        /// 延长锁(续约)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        bool LockExtend(string key, string value, TimeSpan cacheTime);

        /// <summary>
        /// 延长锁(续约)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        Task<bool> LockExtendAsync(string key, string value, TimeSpan cacheTime);

        /// <summary>
        /// 释放锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        bool LockRelease(string key, string value);

        /// <summary>
        /// 释放锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<bool> LockReleaseAsync(string key, string value);

        /// <summary>
        /// 获取锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public bool LockTake(string key, string value, TimeSpan cacheTime);

        /// <summary>
        /// 获取锁
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public Task<bool> LockTakeAsync(string key, string value, TimeSpan cacheTime);

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<string> Get(string key);

        /// <summary>
        /// 获取一个值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        RedisValue StringGet(string key);

        /// <summary>
        /// 搜索获取匹配Key
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        List<string> SearchKey(string pattern);

        /// <summary>
        /// 获取值，并序列化
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<TEntity> Get<TEntity>(string key);

        /// <summary>
        /// 获取一个值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        //保存
        Task<bool> StringSetAsync(string key, string value, TimeSpan cacheTime);

        /// <summary>
        /// 获取一个值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        bool StringSet(string key, string value, TimeSpan cacheTime);

        /// <summary>
        /// 获取一个值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        bool StringSet(string key, string value);

        /// <summary>
        /// 获取一个值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        bool StringSet(string key, RedisValue value);

        /// <summary>
        /// 获取一个值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        bool StringSet(string key, RedisValue value, TimeSpan cacheTime);

        /// <summary>
        /// 设置一个缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        Task SetAsync(string key, object value, TimeSpan cacheTime);

        /// <summary>
        /// Increment 递增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<long> StringIncrement(string key, long value = 1);


        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<bool> KeyExistsAsync(string key);

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool KeyExists(string key);

        /// <summary>
        /// 获取键 过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<TimeSpan?> KeyTime(string key);

        /// <summary>
        /// 删除异步
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task KeyDeleteAsync(string key);
        /// <summary>
        /// 删除key
        /// </summary>
        /// <param name="key"></param>
        void KeyDelete(string key);

        /// <summary>
        /// 模糊删除
        /// </summary>
        /// <param name="pattern"></param>
        void LikeRemove(string pattern);

        /// <summary>
        /// 模糊删除异步
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        Task LikeRemoveAsync(string pattern);

        /// <summary>
        /// 全部清除
        /// </summary>
        /// <returns></returns>
        Task Clear();

        /// <summary>
        /// 根据key获取RedisValue
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        Task<RedisValue[]> ListRangeAsync(string redisKey);

        /// <summary>
        /// 在列表头部插入值。如果键不存在，先创建再插入值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        Task<long> ListLeftPushAsync(string redisKey, string redisValue);

        /// <summary>
        /// 在列表尾部插入值。如果键不存在，先创建再插入值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        Task<long> ListRightPushAsync(string redisKey, string redisValue);

        /// <summary>
        /// 在列表尾部插入数组集合。如果键不存在，先创建再插入值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <returns></returns>
        Task<long> ListRightPushAsync(string redisKey, IEnumerable<string> redisValue);

        /// <summary>
        /// 移除并返回存储在该键列表的第一个元素  反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        Task<T> ListLeftPopAsync<T>(string redisKey) where T : class;

        /// <summary>
        /// 移除并返回存储在该键列表的最后一个元素   反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        Task<T> ListRightPopAsync<T>(string redisKey) where T : class;

        /// <summary>
        /// 移除并返回存储在该键列表的第一个元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        Task<string> ListLeftPopAsync(string redisKey);

        /// <summary>
        /// 移除并返回存储在该键列表的最后一个元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        Task<string> ListRightPopAsync(string redisKey);

        /// <summary>
        /// 列表长度
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        Task<long> ListLengthAsync(string redisKey);

        /// <summary>
        /// 返回在该列表上键所对应的元素
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        Task<IEnumerable<string>> ListRangeAsync(string redisKey, int db = -1);

        /// <summary>
        /// 根据索引获取指定位置数据
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <returns></returns>
        Task<IEnumerable<string>> ListRangeAsync(string redisKey, int start, int stop);

        /// <summary>
        /// 删除List中的元素 并返回删除的个数
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="redisValue"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        Task<long> ListDelRangeAsync(string redisKey, string redisValue, long type = 0);

        /// <summary>
        /// 清空List
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        Task ListClearAsync(string redisKey);


        /// <summary>
        /// 有序集合/定时任务延迟队列用的多
        /// </summary>
        /// <param name="redisKey">key</param>
        /// <param name="redisValue">元素</param>
        /// <param name="score">分数</param>
        Task<bool> SortedSetAddAsync(string redisKey, string redisValue, double score);

        /// <summary>
        /// 插入zset
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="msg">消息</param>
        /// <param name="time">延迟执行时间</param>
        /// <returns></returns>
        Task<bool> SortedSetAddAsync(string key, string msg, DateTime time);

        /// <summary>
        /// 返回有序集合中指定成员的索引
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<long?> SortedSetRankAsync(string key, string value);

        /// <summary>
        /// 返回有序集合中的分数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        Task<double?> SortedSetScoreAsync(string key, string value);

        /// <summary>
        /// 返回有序集合中的分数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        double? SortedSetScore(string key, string value);
        /// <summary>
        /// 返回有序列表里的数据
        /// </summary>
        /// <param name="redisKey">RedisKey</param>
        /// <param name="start">0 是第一个</param>
        /// <param name="stop">最大分数值</param>
        /// <returns></returns>
        Task<IEnumerable<string>> SortedSetRangeByRankAsync(string redisKey, int start, int stop);

        /// <summary>
        /// 移出序列表里的指定范围数量
        /// </summary>
        /// <param name="redisKey">RedisKey</param>
        /// <param name="start">0 是第一个</param>
        /// <param name="stop">最大分数值</param>
        /// <returns></returns>
        Task<long> SortedSetRemoveRangeByRankAsync(string redisKey, int start, int stop);



        /// <summary>
        /// 返回有序列表里的指定范围数量
        /// </summary>
        /// <param name="redisKey">RedisKey</param>
        /// <param name="start">0 是第一个</param>
        /// <param name="stop">最大分数值</param>
        /// <returns></returns>
        Task<long> SortedSetLengthAsync(string redisKey, long min, long max);



    }
}
